package demo1;

/**
 * @Author liangzai
 * @Description:
 */
public class Test {
    public static void main(String[] args) {
        int[] gas = {1,2,3,4,5};
        int[] cost = {3,4,5,1,2};
        canCompleteCircuit(gas,cost);
    }

    public static int canCompleteCircuit(int[] gas, int[] cost) {
        int count = 0;
        while (count < gas.length) {
            int sum = 0;
            int i = count;
            int j=0;
            while(j < gas.length) {
                sum += gas[i];
                sum -=cost[i];
                if(sum < 0){
                    break;
                }
                i = (i+1) % gas.length;
                j++;
            }
            if (j == gas.length) {
                return count;
            }
            count = count+j+1;
        }
        return -1;
    }
}
